package com.app.service.impl;

import cn.hutool.core.util.IdUtil;
import com.app.mapper.MessageBoxMapper;
import com.app.mapper.UserMapper;
import com.app.mapper.VoucherRecordMapper;
import com.app.model.InviteConfigModel;
import com.app.model.MessageBoxModel;
import com.app.model.UserModel;
import com.app.model.VoucherRecordModel;
import com.app.service.IDividedIntoRecordService;
import com.app.service.InviteConfigService;
import com.app.service.MessageBoxService;
import com.app.utils.CommonHelp;
import com.app.vo.MessageBoxVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * 消息盒子表 服务实现类
 *
 * @author 16693103420
 * @since 2024-02-04
 */
@Service
public class MessageBoxServiceImpl extends ServiceImpl<MessageBoxMapper, MessageBoxModel> implements MessageBoxService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MessageBoxMapper messageBoxMapper;
    @Autowired
    private VoucherRecordMapper voucherRecordMapper;

    //查询用户消息盒子列表
    @Override
    public MessageBoxVo getUserMessageBox(Integer page, Integer limit, Integer userId) {
        Page<MessageBoxModel> page1 = new Page<>(page, limit);
        QueryWrapper<MessageBoxModel> qw = new QueryWrapper<>();
        qw.eq("user_id", userId);
        qw.orderByDesc("create_time");
        Page<MessageBoxModel> messageBoxPage = messageBoxMapper.selectPage(page1, qw);
        List<MessageBoxModel> messageBoxList = messageBoxPage.getRecords();
        List<Integer> integerList = new ArrayList<>();
        if (messageBoxList != null && messageBoxList.size() != 0) {
            for (MessageBoxModel messageBoxModel : messageBoxList) {
                Integer quiltUserId = messageBoxModel.getQuiltUserId();
                UserModel quiltUserModel = userMapper.selectById(quiltUserId);
                if (quiltUserModel != null) {
                    messageBoxModel.setFaceUrl(quiltUserModel.getFaceUrl());
                    messageBoxModel.setUserName(quiltUserModel.getName());
                    messageBoxModel.setPhone(quiltUserModel.getPhone());
                }
            }
            // 遍历消息盒子列表
            for (MessageBoxModel messageBox : messageBoxList) {
                // 检查是否已读
                if (!messageBox.getIfRead()) {
                    // 将更新后的消息盒子对象添加到新列表中
                    integerList.add(messageBox.getId());
                }
            }
        }
        //修改
        // 获取当前时间戳
        Timestamp currentTime = new Timestamp(System.currentTimeMillis());
        if (integerList.size() > 0) {
            messageBoxMapper.updateBatchByIds(integerList, currentTime);
        }
        long count = messageBoxPage.getTotal();
        MessageBoxVo messageBoxVo = new MessageBoxVo();
        messageBoxVo.setList(messageBoxList);
        messageBoxVo.setCount((int) count);
        return messageBoxVo;
    }

    @Autowired
    private InviteConfigService inviteConfigService;

    @Autowired
    private IDividedIntoRecordService dividedIntoRecordService;
//
//    //转账操作
//    @Override
//    public void transferAccounts(Integer userId, Integer quiltUserId, Double voucher) {
//
//        InviteConfigModel configModel=this.inviteConfigService.getById(1);
//
//        UserModel userModel = userMapper.selectById(userId);//被转账人
//        UserModel quiltUserModel = userMapper.selectById(quiltUserId);//转账人
//        //转账人钱扣掉
//        Double quiltUserVoucher = quiltUserModel.getVoucher();
//        quiltUserVoucher = quiltUserVoucher - voucher;
//        quiltUserModel.setVoucher(quiltUserVoucher);
//        userMapper.updateById(quiltUserModel);
//        //被转账人钱增加
//        Double userVoucher = userModel.getVoucher();
//        //参与分成的券
//        Double dividedIntoCoupons = voucher * configModel.getTransactionFeeRatio();
//        this.dividedIntoRecordService.dividingByRegionalAgent(userModel, BigDecimal.valueOf(dividedIntoCoupons), IdUtil.fastSimpleUUID());
//        //券
//        Double vouchersShouldGet = (BigDecimal.valueOf(voucher).subtract(BigDecimal.valueOf(dividedIntoCoupons)).doubleValue());
//
//        userVoucher = userVoucher + vouchersShouldGet;
//        userModel.setVoucher(userVoucher);
//        userMapper.updateById(userModel);
//        //todo 转账有订单号吗，得有一个，好向记录变种插入订单号，转账人和收账人的订单号一样不？
//        //生成转账人益联券使用记录
//        VoucherRecordModel quiltUserVoucherRecord = new VoucherRecordModel();
//        quiltUserVoucherRecord.setName("好友转账");
//        quiltUserVoucherRecord.setTypeName("好友转账");
//        quiltUserVoucherRecord.setContent("向" + userModel.getName() + "转账");
//        String amountVoucher = "-" + voucher + "券";
//        quiltUserVoucherRecord.setVoucher(amountVoucher);
//        quiltUserVoucherRecord.setUserId(quiltUserId);
//        quiltUserVoucherRecord.setCreateTime(CommonHelp.getNowTimestamp());
//        voucherRecordMapper.insert(quiltUserVoucherRecord);
//        //生成被转账人益联券使用记录
//        VoucherRecordModel userVoucherRecord = new VoucherRecordModel();
//        userVoucherRecord.setName("好友转账");
//        userVoucherRecord.setTypeName("好友转账");
//        userVoucherRecord.setContent("收到" + userModel.getName() + "转账");
//        String amountVoucherUser = "+" + voucher + "券";
//        userVoucherRecord.setVoucher(amountVoucherUser);
//        userVoucherRecord.setUserId(userId);
//        userVoucherRecord.setCreateTime(CommonHelp.getNowTimestamp());
//        voucherRecordMapper.insert(userVoucherRecord);
//        //将转账信息添加到消息盒子中
//        MessageBoxModel messageBoxModel = new MessageBoxModel();
//        messageBoxModel.setUserId(userId);
//        messageBoxModel.setQuiltUserId(quiltUserId);
//        messageBoxModel.setTypeName("转账通知");
//        messageBoxModel.setVoucher(voucher);
//        messageBoxModel.setNotes("好友转账");
//        messageBoxModel.setCreateTime(CommonHelp.getNowTimestamp());
//        messageBoxMapper.insert(messageBoxModel);
//    }
}
